package cn.detu.coolshark.mapper;

import cn.detu.coolshark.entity.Product;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Mapper
public interface ProductMapper {

    @Select("select * from product")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    @Result(column = "category_id", property = "categoryId")
    List<Product> select();

    @Insert("insert into product values(null,#{title},#{price},#{oldPrice},#{url},#{saleCount},#{categoryId},0,#{created})")
    void insert(Product product);

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);

    @Select("select id,title,price,old_price,url,sale_count from product")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    List<Product> selectB();

    @Select("select id,title,sale_count from product order by sale_count desc limit 0,6")
    @Result(column = "sale_count", property = "saleCount")
    List<Product> selectC();

    @Select("select id,title,price,old_price,url,sale_count from product where category_id=#{cid}")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    List<Product> selectD(int cid);


    @Select("select id,title,price,old_price,url,sale_count from product where  title like concat('%',#{wd},'%')")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    List<Product> selectE(String  wd);

    @Select("select id,title,price,old_price,url,sale_count from product where  category_id=8")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    List<Product> selectF();

    @Select("select id,title,price,old_price,url,sale_count,view_count,created from product where id=#{id}")
    @Result(column = "old_price", property = "oldPrice")
    @Result(column = "sale_count", property = "saleCount")
    @Result(column = "view_count", property = "viewCount")
    Product selectById(int id);


    @Update("update product set view_count=view_count+1 where id=#{id}")
    void updateByViewCount(int id);

    @Delete("delete from product where category_id=#{id}")
    void deleteByCid(int id);
}
